package com.younion.action.manage;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.business.ICardLevelService;
import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;
import com.younion.vo.CardLevelVO;
import com.younion.vo.CustomerVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author wells
 */
public class CardLevelAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    protected static final Log logger = LogFactory.getLog(CardLevelAction.class);

    private ICardLevelService cardLevelService = null;

    private CardLevelVO cardLevelVO = new CardLevelVO();

    private List<CardLevelVO> cardLevelList;// 结果集列表

    /**
     * 列表：翻页区；数据区
     */
    @SuppressWarnings("unchecked")
    public String listCardLevel() {
        try {
            Map paramMap = null;
            if (getSessionUser().getCustomer() != null) {
                cardLevelVO.setCustomerVO(getSessionUser().getCustomer());
            } else if (cardLevelVO != null && cardLevelVO.getCustomerId() != null) {
                cardLevelVO.setCustomerVO(new CustomerVO(cardLevelVO.getCustomerId()));
            }
            if (cardLevelVO != null) {
                paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(cardLevelVO));
            }
            // 获取总记录数
            int total = cardLevelService.getCountByMap(paramMap);
            // 根据总记录数获取分页对象
            Paginator paginator = this.getPaginator(total);
            cardLevelList = cardLevelService.get(paramMap, paginator);
            // 保存分页对象
            this.savaPaginator(paginator);
            this.addLog(2, "查询会员卡级别列表", "查询会员卡级别列表", this.getSessionUser());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return SUCCESS;
    }

    /**
     * 新增
     */
    public String addCardLevel() {
        return SUCCESS;
    }

    /**
     * 修改保存，调度Jsp
     */
    public String editCardLevel() {
        try {
            cardLevelVO = cardLevelService.getById(cardLevelVO.getId().toString());
            Map<String, CustomerVO> paramMap = new HashMap<String, CustomerVO>();
            CustomerVO cust = new CustomerVO();
            cust.setId(cardLevelVO.getCustomerVO().getId());
            paramMap.put("customerVO", cust);
            this.cardLevelList = (List<CardLevelVO>) this.cardLevelService.getByMap(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 保存动作
     */
    public String saveCardLevel() {
        try {
            cardLevelVO = cardLevelService.save(cardLevelVO);
            Integer customerId = getSessionUser().getCustomer().getId() == null ? -1 : getSessionUser().getCustomer().getId();
            memoryCacheManager.invalidateCardLevel(customerId);
            String id = cardLevelVO.getId() == null ? "" : cardLevelVO.getId().toString();
            String log = "".equals(id) ? "添加会员卡级别" : "更新会员卡级别";
            int type = "".equals(id) ? 3 : 4;
            this.addLog(type, log + id, log, getSessionUser());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        request.setAttribute("refreshNavTabId", "cardLevleList");
        return SUCCESS;
    }

    /**
     * 删除
     */
    public String deleteCardLevel() {
        try {
            String pkId = request.getParameter("ids");
            cardLevelService.remove(Arrays.asList(pkId.split(",")));
            Integer customerId = getSessionUser().getCustomer() == null ? -1 : getSessionUser().getCustomer().getId();
            memoryCacheManager.invalidateCardLevel(customerId);
            String log = "删除会员卡级别";
            this.addLog(5, log + pkId, log, getSessionUser());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        request.setAttribute("refreshNavTabId", "cardLevleList");
        return SUCCESS;
    }

    /**
     * 查看
     */
    public String viewCardLevel() {
        try {
            String pkId = request.getParameter("ids");// 获得传递过来主键;
            cardLevelVO = cardLevelService.getById(pkId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return SUCCESS;

    }

    public CardLevelVO getCardLevelVO() {
        return cardLevelVO;
    }

    public void setCardLevelVO(CardLevelVO cardLevelVO) {
        this.cardLevelVO = cardLevelVO;
    }

    public void setCardLevelService(ICardLevelService cardLevelService) {
        this.cardLevelService = cardLevelService;
    }

    public List<CardLevelVO> getCardLevelList() {
        return cardLevelList;
    }

    public void setCardLevelList(List<CardLevelVO> cardLevelList) {
        this.cardLevelList = cardLevelList;
    }

}